package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.work.impl.dao.ProductionReportDao;
import com.jiantong.work.impl.dao.PurchaseRecordDao;
import com.jiantong.work.model.PurchaseRecord;
import com.jiantong.work.service.PurchaseRecordReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * Created by maowenjie on 2017/3/20.
 */
@Slf4j
@Service
@RpcProvider
public class PurchaseRecordReadServiceImpl implements PurchaseRecordReadService{

    private final PurchaseRecordDao purchaseRecordDao;

    @Autowired
    public PurchaseRecordReadServiceImpl(PurchaseRecordDao purchaseRecordDao){
        this.purchaseRecordDao = purchaseRecordDao;
    }

    @Override
    public Response<PurchaseRecord> findById(Long id) {
        try{
            PurchaseRecord purchaseRecord = purchaseRecordDao.findById(id);
            if(purchaseRecord == null){
                log.error("purchaseRecord(id={}) is not found", id);
                return Response.fail("purchaseRecord.not.found");
            }
            return Response.ok(purchaseRecord);
        }catch (Exception e){
            log.error("failed to find purchaseRecord(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("purchaseRecord.find.fail");
        }
    }

    @Override
    public Response<Map<String, Object>> findStatusById(Long id) {
        try{
            Integer status = purchaseRecordDao.findStatusById(id);
            Map<String , Object> map = Maps.newHashMap();
            if(status == null){
                log.error("purchaseRecord status (id={}) is not found", id);
                return Response.fail("purchaseRecord.status.not.found");
            }
            map.put("status",status);
            return Response.ok(map);
        }catch (Exception e){
            log.error("purchaseRecord status (id={}) is not found", id);
            return Response.fail("purchaseRecord.status.not.found");
        }
    }

    @Override
    public Response<Paging<PurchaseRecord>> pagingPurchaseRecord(Integer pageNo, Integer pageSize) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            return Response.ok(purchaseRecordDao.paging(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find purchaseRecord, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("purchaseRecord.find.fail");
        }
    }

    @Override
    public Response<Paging<PurchaseRecord>> pagingRecord(Integer pageNo, Integer pageSize,Integer status,Long factoryId, Date fromDate,Date toDate) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            if (status !=null){
                criteria.put("status",status);
            }
            if(factoryId != null){
                criteria.put("factoryId",factoryId);
            }
            if (fromDate!=null && toDate!=null){
                criteria.put("fromDate",fromDate);
                criteria.put("toDate",toDate);
            }
            return Response.ok(purchaseRecordDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to find PurchaseRecord(factoryId={},fromDate={},toDate={}), cause:{}", factoryId, fromDate,toDate,Throwables.getStackTraceAsString(e));
            return Response.fail("PurchaseRecord.find.fail");
        }
    }
}
